package cn.xiaolang.thread.lock;

/**
 * @author fuhw/Dean
 * @date 2020-03-06
 */
public class ReadWriteLockStarted {


    public static void main(String[] args) {
        readWrite();
    }

    /**
     * 读写互斥
     * <pre>
     * 17:52:48.261 [t1] INFO cn.xiaolang.thread.lock.ReadWriteResource - read start:
     * 17:52:53.274 [t1] INFO cn.xiaolang.thread.lock.ReadWriteResource - read end!
     * 17:52:53.275 [t2] INFO cn.xiaolang.thread.lock.ReadWriteResource - write start:
     * 17:52:58.275 [t2] INFO cn.xiaolang.thread.lock.ReadWriteResource - write end!
     * </pre>
     */
    private static void readWrite() {
        ReadWriteResource resource = new ReadWriteResource();
        new Thread(resource::read, "t1").start();

        new Thread(resource::write, "t2").start();
    }

    /**
     * 写写互斥
     * <pre>
     * 17:53:50.461 [t1] INFO cn.xiaolang.thread.lock.ReadWriteResource - write start:
     * 17:53:55.466 [t1] INFO cn.xiaolang.thread.lock.ReadWriteResource - write end!
     * 17:53:55.466 [t2] INFO cn.xiaolang.thread.lock.ReadWriteResource - write start:
     * 17:54:00.472 [t2] INFO cn.xiaolang.thread.lock.ReadWriteResource - write end!
     * </pre>
     */
    private static void writeWrite() {
        ReadWriteResource resource = new ReadWriteResource();
        new Thread(resource::write, "t1").start();

        new Thread(resource::write, "t2").start();
    }

    /**
     * 读读共享
     * <pre>
     * 17:54:25.938 [t1] INFO cn.xiaolang.thread.lock.ReadWriteResource - read start:
     * 17:54:25.938 [t2] INFO cn.xiaolang.thread.lock.ReadWriteResource - read start:
     * 17:54:30.942 [t2] INFO cn.xiaolang.thread.lock.ReadWriteResource - read end!
     * 17:54:30.942 [t1] INFO cn.xiaolang.thread.lock.ReadWriteResource - read end!
     * </pre>
     */
    private static void readRead() {
        ReadWriteResource resource = new ReadWriteResource();
        new Thread(resource::read, "t1").start();

        new Thread(resource::read, "t2").start();
    }


}
